#ifndef _RSS_DB_H_
#define _RSS_DB_H_
#include "../sqlite/sqlite3.h"
#include <string>
#include <list>
#include <iostream>
#include "std_ex.h"
#include "../ui/Rss_UI_MyList.h"
using namespace std;

class Rss_PageInfo;
class Rss_DB{
private:
	sqlite3* p_db;
	sqlite3_stmt* p_stmt;
	bool is_opened;
	bool is_prepared;
	string db_name;
	bool isInitial() const;
public:
	Rss_DB();
	~Rss_DB();
	bool Open();
	void Close();
	bool SetSQL(const string& sql);
//	bool BindParameter(int index,int value);
//	bool BindParameter(int index,const string& value);
	bool ExecuteNoQuery();
	string ExecuteScalar();
public:
	static void SaveUser(const wstring& user,const wstring& pwd);
	static wstring GetUser();
	static wstring GetPwd();
	static void SaveArticle(const MyListItemData* p_data);
	static void DeleteArticle(int itemId);
	static bool ArticleExists(int itemId);
	static list<MyListItemData> GetItemData_Page(const string& sql,int page,Rss_PageInfo* pageInfo,int pageSize = 50);
	static void CreateDB();
	static int TotalArticle();
};
class Rss_PageInfo{
public:
	int RecordCount;
	int PageCount;
	int PageSize;
	int CurrentPage;
	bool IsFirst;
	bool IsLast;
public:
	Rss_PageInfo(){
		this->RecordCount = 0;
		this->PageCount = 0;
		this->PageSize = 0;
		this->CurrentPage = 0;
		this->IsFirst = false;
		this->IsLast = false;
	}
	void Caculate(){
		this->PageCount = this->RecordCount / this->PageSize;
		if(this->RecordCount % this->PageSize) this->PageCount += 1;
		if(this->CurrentPage <= 1){
			this->IsFirst = true;
			this->CurrentPage = 1;
		}
		if(this->CurrentPage >= this->PageCount){
			this->IsLast = true;
			this->CurrentPage = this->PageCount;
		}
	}
	int GetValidPage(){
		return this->CurrentPage;
	}
	int GetValidStartIndex(){
		return (this->CurrentPage - 1) * this->PageSize;
	}
};
#endif
